package com.frxuexi.document.view

import android.os.Environment
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Send
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil3.compose.AsyncImage
import com.baidu.entity.pb.PoiResult.Contents.Show
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.document.DocIntent
import com.frxuexi.document.DocViewModel
import com.frxuexi.lib_base.BaseApp
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RequestType
import com.frxuexi.lib_base.UiState
import kotlinx.coroutines.launch
import java.io.File

@Composable
fun LocalDocPage(navHostController: NavHostController,viewModel: DocViewModel= hiltViewModel()) {
    var list = remember {
        SnapshotStateList<File>()
    }
    var checkedName by remember {
        mutableStateOf("")
    }
    var path by remember {
        mutableStateOf("")
    }
    val file = BaseApp.context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!
    LaunchedEffect(key1 = "", block = {
        launch {
            viewModel.state.collect{
                when(it)
                {
                    is UiState.onSuccess<*> ->{
                        if(it.type==RequestType.SCAN) {
                            val files = it.data as Collection<File>
                            val imgs = arrayOf("png", "jpg", "jpeg")
                            list.addAll(files.filter {
                                imgs.contains(it.name.split(".")[1].lowercase())
                            })
                        }else{
                            ToastUtils.showLong("上传成功")
                            navHostController.popBackStack()
                        }
                    }

                    else -> {}
                }
            }
        }

//        viewModel.sendIntent(DocIntent.scanFile(File("/sdcard/DCIM/Camera")))
        viewModel.sendIntent(DocIntent.scanFile(file))
    })

    Column(Modifier.padding(20.dp)) {
        BaseTitle(navHostController = navHostController, title = "本地文件选择",btn="上传"){
            viewModel.sendIntent(DocIntent.upload(checkedName,path))
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn( content = {
            items(list){
                FileItem(file = it,checkedName==it.name){
                    checkedName=it.name
                    path=it.path
                }
            }
        })
    }
}

@Composable
fun FileItem(file: File,isCheck:Boolean,onClick: () -> Unit){
    Row{
        Box(modifier = Modifier.padding(top = 10.dp),contentAlignment= Alignment.TopEnd){
            Image(painter = painterResource(id = R.drawable.icon3), contentDescription = "", modifier = Modifier.size(60.dp))
            var img=if(isCheck) com.frxuexi.document.R.drawable.ic_action_check else com.frxuexi.document.R.drawable.ic_action_uncheck
            Image(painter = painterResource(id = img), contentDescription = "",
                modifier = Modifier
                    .size(16.dp)
                    .offset(2.dp, -5.dp)
                    .clickable {
                        onClick()
                    })
        }

        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(60.dp), verticalArrangement = Arrangement.SpaceAround) {
            val name = file.name.split(".")[0]
            Text(text = name,maxLines = 1, overflow = TextOverflow.Ellipsis)
            Text(text = file.path, maxLines = 1, overflow = TextOverflow.Ellipsis)
        }
    }
}